package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.SmsCoupon;
import com.itheima.mapper.SmsCouponMapper;
import com.itheima.service.db.SmsCouponService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsCouponServiceImpl implements SmsCouponService {

    @Autowired
    SmsCouponMapper smsCouponMapper;

    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize,String name,Integer type) {
        Page<SmsCoupon> page = new Page<>(pageNum,pageSize);

        LambdaQueryWrapper<SmsCoupon> qw = new LambdaQueryWrapper<>();

        qw.like(StrUtil.isNotEmpty(name),SmsCoupon::getName,name);
        qw.eq(type != null,SmsCoupon::getType,type);

        Page<SmsCoupon> smsCouponPage = smsCouponMapper.selectPage(page, qw);

        return new PageBeanVo(pageNum,pageSize,smsCouponPage.getTotal(),smsCouponPage.getRecords());
    }

    @Override
    public SmsCoupon findById(Long couponId) {
        return smsCouponMapper.selectById(couponId);
    }

    @Override
    public void updateById(SmsCoupon smsCoupon) {
        smsCouponMapper.updateById(smsCoupon);
    }

    @Override
    public void deleteById(Long id) {
        smsCouponMapper.deleteById(id);
    }

    @Override
    public void save(SmsCoupon smsCoupon) {
        smsCouponMapper.insert(smsCoupon);
    }
}
